﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.QuanTri
{
    public partial class QLPhanMem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPhongHoc();
            }
        }

        protected void rpPhongHoc_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                H_PhongHocId.Value = id.ToString();

                litPhong.Text = ": " + DataRepository.PhongHocProvider.GetByMaPhong(id).TenPhong;
                BindPhanMem();
            }
        }

        protected void rpPhanMem_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                ImageButton btn = (ImageButton)e.CommandSource;
                if (btn.ImageUrl == "~/Images/unchecked_checkbox.png")
                {
                    btn.ImageUrl = "~/Images/checked_checkbox.png";
                }
                else
                {
                    btn.ImageUrl = "~/Images/unchecked_checkbox.png";
                }
            }
        }

        protected void cmdLuu_OnClick(object sender, EventArgs e)
        {
            bool status = false;
            for (int i = 0; i < rpPhanMem.Items.Count; i++)
            {
                string maPhanMem = ((HiddenField)rpPhanMem.Items[i].FindControl("H_MaPhanMem")).Value;
                ImageButton cmdSelect = ((ImageButton)rpPhanMem.Items[i].FindControl("cmdSelect"));
                PhongHocPhanMem ent_PHPM = new PhongHocPhanMem();
                ent_PHPM.MaPhong = Convert.ToInt32(H_PhongHocId.Value);
                ent_PHPM.MaPhanMem = Convert.ToInt32(maPhanMem);
                if (cmdSelect.ImageUrl == "~/Images/checked_checkbox.png")
                {

                    if (getAnh(maPhanMem) == true)
                    {
                        ent_PHPM.MaPhpm = getMaPhongHocPhanMem(maPhanMem);
                        status = DataRepository.PhongHocPhanMemProvider.Update(ent_PHPM);
                    }
                    else
                    {
                        status = DataRepository.PhongHocPhanMemProvider.Insert(ent_PHPM);
                    }
                }
                else
                {
                    if (getAnh(maPhanMem) == true)
                    {
                        status = DataRepository.PhongHocPhanMemProvider.Delete(getMaPhongHocPhanMem(maPhanMem));
                    }
                }
               
            }

            if (!status)
            {
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.CAPNHAT_THANHCONG, true);
            }
        }

        private void BindPhongHoc()
        {
            TList<PhongHoc> TList_PhongHoc = DataRepository.PhongHocProvider.GetAll();
            rpPhongHoc.DataSource = TList_PhongHoc;
            rpPhongHoc.DataBind();
        }
        private void BindPhanMem()
        {
            TList<PhanMem> TList_PhanMem = DataRepository.PhanMemProvider.GetAll();
            rpPhanMem.DataSource = TList_PhanMem;
            rpPhanMem.DataBind();
        }


        public string getTenCanBo(object ob)
        {
            string id = ob.ToString();
            return DataRepository.CanBoProvider.GetByMaCanBo(id).HoTen;

        }

        public bool getAnh(object pm)
        {
            string maPM = pm.ToString();
            string maPH = H_PhongHocId.Value;
            int totalRow = 0;
            string whereClause = "MaPhanMem = " + maPM + " and MaPhong=" + maPH;
            TList<PhongHocPhanMem> TList_PHPM = DataRepository.PhongHocPhanMemProvider.GetPaged(whereClause, "", 0, 0,
                out totalRow);
            if (TList_PHPM.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int getMaPhongHocPhanMem(object pm)
        {
            string maPM = pm.ToString();
            string maPH = H_PhongHocId.Value;
            int totalRow = 0;
            string whereClause = "MaPhanMem = " + maPM + " and MaPhong=" + maPH;
            TList<PhongHocPhanMem> TList_PHPM = DataRepository.PhongHocPhanMemProvider.GetPaged(whereClause, "", 0, 0,
                out totalRow);
            if (TList_PHPM.Count > 0)
            {
                return TList_PHPM[0].MaPhpm;
            }
            else
            {
                return 0;
            }
        }

    }
}